//
//  SecondViewController.m
//  任务列表练习
//
//  Created by apple on 15/9/16.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "SecondViewController.h"
#import "Masonry.h"
#import "ImageView.h"
#import "ThreeViewController.h"
#import "DateManager.h"


@interface SecondViewController ()

{
    DateManager *dataManager;
}

@end

@implementation SecondViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    CGSize size = self.view.frame.size;
    
    self.title = @"添加新任务";
   
    self.view.backgroundColor = [UIColor whiteColor];
    
    //导航条左
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(goBackClick:)];
    
    //导航条右
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Enter"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(didAddClicked:)];
    
    
    _TextField = [[UITextField alloc]init];
    
//    _TextField.backgroundColor = [UIColor orangeColor];
    
    _TextField
    
    [self.view addSubview:_TextField];
   
    //任务标题的位置（10，75，size.width - 20,20）;
    [_TextField mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.equalTo(@75);
        make.left.equalTo(@10);
        make.size.equalTo(MASBoxValue(CGSizeMake(size.width - 20, 20)));
    }];
    
    //内容文本框的位置
    
    _textView = [[UITextView alloc] init];
    
    _textView.backgroundColor = [UIColor clearColor];


    [self.view addSubview:_textView];
    
    [_textView mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.equalTo(_TextField.mas_bottom).offset(10);
         
         make.left.equalTo(@10);
         
         make.size.equalTo(MASBoxValue(CGSizeMake(size.width - 20, 200)));
     }];
    
    
    //文本下面的图标
   
    ImageView *imageView = [[ImageView alloc] init];
    
    imageView.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:imageView];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.equalTo(_textView.mas_bottom).offset(0);
         make.left.equalTo(@(10));
         make.size.equalTo(MASBoxValue(CGSizeMake(size.width - 20, 30)));
     }];
    
    
    //图片的选择
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"5"]];
    
    [self.view addSubview:imageView1];
    
    [imageView1 mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.equalTo(imageView.mas_bottom).offset(20);
        make.left.equalTo(@(10));
        make.size.equalTo(MASBoxValue(CGSizeMake(150, 150)));
    }];
}

- (void)didAddClicked:(UIButton *)sender
{
    dataManager = [DateManager new];
    
    dataManager.title = _TextField.text;
    
    dataManager.content = _textView.text;
    
    if (dataManager.array == nil)
    {
        dataManager.array = [NSMutableArray array];
        
        dataManager.dict = [NSMutableDictionary dictionary];
        
        [dataManager.dict setObject:_TextField.text forKey:@"title"];
        
        [dataManager.dict setObject:_textView.text forKey:@"content"];
        

    }
    else
    {
        if (dataManager.dict == nil)
        {
            dataManager.dict = [NSMutableDictionary dictionary];
        }
        
        [dataManager.dict setObject:_TextField.text forKey:@"title"];
        
        [dataManager.dict setObject:_textView.text forKey:@"content"];
    
    }

    [dataManager.array addObject:dataManager.dict];
    
    [dataManager.array writeToFile:@"/Users/apple/Desktop/123.text" atomically:YES];
    
    _textView.text = nil;
    
    _TextField.text = nil;
    
    dataManager.dict = nil;

    
//    SecondViewController *secondViewCtrl = [SecondViewController new];
    
    NSMutableArray *arr = dataManager.array;
    
    self.viewCtrl.reArray = arr;
    
    [self.viewCtrl.tableView reloadData];
    
    
    ThreeViewController *threeViewCtrl = [[ThreeViewController alloc] init];
    
    threeViewCtrl.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)goBackClick:(UIButton *)sender
{
//    [DateManager new].title = _TextField.text;
    
    [self.navigationController popViewControllerAnimated:YES];
    
//    _textView.text = nil;
    
//    _TextField.text = nil;
}

@end
